package com.mysoft.gfyx.clue.controller;

import com.mysoft.framework.modeling.dataapi.DataApi;
import com.mysoft.framework.mvc.api.Controller;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.clue.controller.dto.Perm.GetUserBUDataPermissionRequestDto;
import com.mysoft.gfyx.clue.controller.dto.Perm.ImportUserBUDataPermissionResponseDto;
import com.mysoft.gfyx.clue.controller.dto.Perm.SaveUserBUDataPermissionRequestDto;
import com.mysoft.gfyx.clue.controller.dto.Perm.ImportUserBUDataPermissionRequestDto;
import com.mysoft.gfyx.clue.service.DataPermissionService;
import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.util.ArrayList;

@PubService(value = "/dataPermissionDataApi", prefix = RequestPrefix.API)
@Tag(name = "数据权限-API")
public class DataPermissionDataApiController extends Controller implements DataApi {

    @Autowired
    private DataPermissionService dataPermissionService;

    @PubAction(value = "/saveUserBUDataPermission", method = RequestMethod.POST)
    @ApiOperation(value = "/saveUserBUDataPermission", notes = "保存bu的区域权限，并且将权限给到一批用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "users", value = "用户", paramType = "query", dataType = "List<UUID>", required = true),
            @ApiImplicitParam(name = "BUGUID", value = "公司GUID", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "area", value = "地区,用code逗号隔开", paramType = "query", dataType = "String", required = true),
    })
    public Object saveUserBUDataPermission(@RequestBody SaveUserBUDataPermissionRequestDto saveUserBUDataPermissionRequestDto) {
        dataPermissionService.saveUserBUDataPermission(DataPermissionService.DataPermissionType.CustomerClue,
                saveUserBUDataPermissionRequestDto.getBUGUID(),
                DataPermissionService.BUDataPermissionType.Area,
                saveUserBUDataPermissionRequestDto.getArea(),
                saveUserBUDataPermissionRequestDto.getUsers());

        return "Success";
    }

    @PubAction(value = "/getUserBUDataPermission", method = RequestMethod.POST)
    @ApiOperation(value = "/getUserBUDataPermission", notes = "保存bu的区域权限，并且将权限给到一批用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "BUGUID", value = "公司GUID", paramType = "query", dataType = "String", required = true),
    })
    public Object getUserBUDataPermission(@RequestBody GetUserBUDataPermissionRequestDto getUserBUDataPermissionRequestDto) {
        return dataPermissionService.getUserBUDataPermission(DataPermissionService.DataPermissionType.CustomerClue,
                getUserBUDataPermissionRequestDto.getAliasBUGUID());
    }

    @PubAction(value = "/importUserBUDataPermission", method = RequestMethod.POST)
    public Object importUserBUDataPermission(@RequestBody ImportUserBUDataPermissionRequestDto dto) {
        try {
            return dataPermissionService.importUserBUDataPermission(dto);
        } catch (Exception e) {
            return "导入失败，错误信息：" + e.getMessage();
        }
    }
}
